package com.api.department.searchdocuments;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IDepartmentApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.DocumentApi;
import com.zyncro.api.model.PagedItemsResultApi;

@ContextConfiguration(locations = "classpath:/department/departments-toget.xml")
public class SearchDocuments extends AbstractApiTest {

	@Value("#{user_contact.appId}")
	private String userUrn;

	@Value("#{user_contact.password}")
	private String userPassword;

	@Value("#{org_toget.searchText}")
	private String text;

	@Value("#{department_toGet.urn}")
	private String iddepartment;

	@Value("#{org_toget.totalSearchDocuments}")
	private int totalSearchDocuments;

	@Value("#{user_contact.totalOwnedDocuments}")
	private int totalOwnedDocuments;

	@Value("#{department_toGet.totalDepartmentDocuments}")
	private int totalDepartmentDocuments;

	@BeforeClass
	protected void loginUser() throws Exception {

		getApi().login(userUrn, userPassword);
	}

	@Test
	public void testSearchDocumentsAll() throws ZyncroApiException {
		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> departments = null;
		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IDepartmentApiService.class).searchDocuments(pageNumber,
				itemsPerPage, text, departments, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), totalSearchDocuments, "Total seach documents");
	}

	@Test
	public void testSearchDocumentsOnlySize() throws ZyncroApiException {
		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> departments = null;
		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = true;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IDepartmentApiService.class).searchDocuments(pageNumber,
				itemsPerPage, text, departments, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), totalSearchDocuments, "Total seach documents");
		Assert.assertEquals(result.getElements().size(), 0, "Elements size");
	}

	@Test
	public void testSearchDocumentsUser() throws ZyncroApiException {
		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> departments = null;

		List<String> users = new ArrayList<String>();
		users.add(userUrn);

		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IDepartmentApiService.class).searchDocuments(pageNumber,
				itemsPerPage, text, departments, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), totalOwnedDocuments, "Total seach owned documents");
	}

	@Test
	public void testSearchDocumentsDepartments() throws ZyncroApiException {
		int pageNumber = 0;
		int itemsPerPage = 0;

		List<String> departments = new ArrayList<String>();
		departments.add(iddepartment);

		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IDepartmentApiService.class).searchDocuments(pageNumber,
				itemsPerPage, text, departments, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), totalDepartmentDocuments, "Total seach departments documents");
	}
}
